package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.DecorateNoteClickDAO;
import com.daxia.bumall.dto.DecorateNoteClickDTO;
import com.daxia.bumall.model.DecorateNoteClick;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class DecorateNoteClickService {
	
	@Autowired
	private DecorateNoteClickDAO decorateNoteClickDAO;
	
	public List<DecorateNoteClickDTO> find(DecorateNoteClickDTO query, Page page) {
		List<DecorateNoteClick> models = decorateNoteClickDAO.find(query, page);
		List<DecorateNoteClickDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DecorateNoteClickDTO> findAll() {
		return this.find(new DecorateNoteClickDTO(), null);
	}
	
	public List<DecorateNoteClickDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DecorateNoteClickDTO(), page);
	}
	
	public Long create(DecorateNoteClickDTO dto) {
		DecorateNoteClick model = new DecorateNoteClick();
		toModel(model, dto);
		return decorateNoteClickDAO.create(model);
	}
	
	public DecorateNoteClickDTO load(Long id) {
	    DecorateNoteClick model = decorateNoteClickDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DecorateNoteClickDTO dto) {
		DecorateNoteClick model = decorateNoteClickDAO.load(dto.getId());
		toModel(model, dto);
		decorateNoteClickDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				decorateNoteClickDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DecorateNoteClickDTO findOne(DecorateNoteClickDTO query) {
		DecorateNoteClick model = decorateNoteClickDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DecorateNoteClickDTO> toDTOs(List<DecorateNoteClick> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DecorateNoteClickDTO>(0);
		}
		List<DecorateNoteClickDTO> dtos = new ArrayList<DecorateNoteClickDTO>(models.size());
		for (DecorateNoteClick model : models) {
	        DecorateNoteClickDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DecorateNoteClickDTO toDTO(DecorateNoteClick model) {
		if (model == null) {
			return null;
		}
		DecorateNoteClickDTO dto = BeanMapper.map(model, DecorateNoteClickDTO.class);
		
		return dto;
	}
	
	private void toModel(DecorateNoteClick model, DecorateNoteClickDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<DecorateNoteClick> toModels(List<DecorateNoteClickDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<DecorateNoteClick>(0);
		}
		List<DecorateNoteClick> models = new ArrayList<DecorateNoteClick>(dtos.size());
		for (DecorateNoteClickDTO dto : dtos) {
	        DecorateNoteClick model = new DecorateNoteClick();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
